<?php

declare(strict_types=1);

/**
 * User：Abell
 * Date：2020/5/16
 */

namespace App\Controller\Abell;

use App\Controller\AbstractController;
use App\Model\Channel as ChannelModel;
use App\Utils\ApiUtils;
use App\Utils\Excel;
use App\Utils\ServerUtil;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Controller;
//
use \Hyperf\Config\Config;

/**
 * @Controller()
 */
class AutotestController extends AbstractController
{
    /** 获取数据库数据 默认导出昨天的数据
     * @RequestMapping(path="/abell/autotest/abell", methods="get,post")
     */
    public function abell(){

    }

    /** 获取数据库数据 默认导出昨天的数据
     * @RequestMapping(path="/abell/autotest/output", methods="get,post")
     */
    public function output(RequestInterface $request){
        $start_time = $request->input('start_time',strtotime(date('Y-m-d')) - (60 * 60 * 24));
        $end_time = $request->input('end_time',strtotime(date('Y-m-d', time())));
        $pageSize = (int)$request->input('page_size',10000);
        // 获取数据库数据
        $count_data = ChannelModel::query()->whereBetween('add_time',[(int)$start_time,(int)$end_time])->count();
        $filename="channel";
        $unquieName=Excel::genZipName();
        $page = ceil($count_data/$pageSize);
        $check_page =1;
        while ($check_page <= (int)$page){
            $head=['channel' => '渠道名称' , 'money' => '金额' ,'phone' => '手机号' ,'callback_url' => '回调域名','phone_type' => '手机号类型',
                'order_no' => '排序' , 'phone_area' => '手机号归属地' ,'pay_money' => '支付金额' ,'operator_order_id' => '订单号','pay_status' => '支付状态',
                'ext' => 'ext' , 'merchant_no' => '是否发货' ,'channel_id' => '渠道id' ,'add_time' => '添加时间'];
            $data = ChannelModel::query()->orderBy('add_time','asc')
                ->whereBetween('add_time',[(int)$start_time,(int)$end_time])
                ->paginate(intval($pageSize),['*'],'page',(int)$check_page)->toArray();
            $files[]=Excel::create($head,$data['data'])->save();
            $check_page++;
        }
        if(empty($files))  return false;
        Excel::zipFiles($unquieName,$files);
        $url = Excel::downloadLink($filename,$unquieName,"/api/channel_api/static/excel");
        return $this->response->json(ApiUtils::send(0,'ok',$url,$count_data));
    }

    /**
     * 下载excel文档
     * @RequestMapping(path="/channel_api/static/excel", methods="get,post")
     */
    public function excelDownload()
    {
        try {
            $filename = $this->request->input('filename', '');
            $uniqueName = $this->request->input('uniqueName', '');
            if (empty($filename) || empty($uniqueName)) {
                $this->response->json(ApiUtils::send(600, '参数错误'));
            }
            //判断文件是否存在
            $file=BASE_PATH . Excel::getFileDir() . '/' . $uniqueName;
            if(file_exists($file)){
                return $this->response->download($file, $filename . ".".pathinfo($file)['extension']);
            }else{
                return $this->response->json(ApiUtils::send(600, '未检索到文件存在(注：数据内容过大请稍后再试)'));
            }
        } catch (\Throwable $t) {
            ServerUtil::info('首页-下载excel文档',['param'=>$this->request->all(),'path'=>$this->request->getPathInfo(),'res'=>$t->getMessage()]);
            return $this->response->json(ApiUtils::send(600, '下载异常'));
        }
    }

    /**
     *  批量复制协程数组执行代码
     * @RequestMapping(path="/abell/autotest/hello", methods="get,post")
     */
    public function hello(){
        return 'hello kitty';
    }
}

